
/**
 * 弹出框
 * 移动端使用
 **/
var mdialog = {
    alter: function (message, time) {
        if (!time)
            time = 2;
        layer.open({
            content: message
            ,btn: '确定'
            ,time: time
        });
    },
    show: function (message, time, func) {
        if (!time)
            time = 2;
        layer.open({
            content: message
            ,skin: 'msg'
            ,time: time //time秒后自动关闭
            ,success: function(){
                if(func) func();
            }
        });
    },
    confirm: function (message, sfunc, efunc, btns) {
        if(!btns){
            btns = ['是','否'] //按钮
        }
        layer.open({
            content: message
            ,btn: btns
            ,yes: function(index){
                sfunc();
                layer.close(index);
            }
            ,no: function(index){
                if(efunc)
                    efunc();
                layer.close(index);
            }
        });
    }
}

function goUrl(url){
    // var ParHref = parsURL().params;
    // var paid = ParHref.paid, urlTmn = url.indexOf('paid');
    // if(paid) {
    //     if(urlTmn == -1){
    //         url = url.indexOf("?") != -1 ? url+"&paid="+paid : url+"?paid="+paid;
    //     }else{
    //         url = url;
    //     }
    // }
    window.location.href = url;
};

function getUrl(url){
    // url = arguments[0] == undefined ? window.location.pathname : url;
    // var ParHref = parsURL().params;
    // var paid = ParHref.paid, urlTmn = url.indexOf('paid');
    // if(paid) {
    //     if(urlTmn == -1){
    //         url = url.indexOf("?") != -1 ? url+"&paid="+paid : url+"?paid="+paid;
    //     }else{
    //         url = url;
    //     }
    // }
    return url;
}


var nform = {
    submit: function (formId, url, succFunc, errFunc) {
        $.ajax({
            type : "POST",
            url:url,
            data : $('#'+formId).serialize(),// 你的formid
            success: function(data){
                if(data.status == 1){
                    if(data.info){
                        ndialog.show(data.info);
                    }else{
                        ndialog.show('操作成功');
                    }
                    if(succFunc) succFunc();
                }else{
                    if(data.info){
                        ndialog.show(data.info);
                    }else{
                        ndialog.show('操作失败');
                    }
                    if(errFunc) errFunc();
                }
            }
        },'json');
    },
    delete: function(obj, msg, succFunc, errFunc){
        if(!msg){
            msg = '确认删除';
        }
        if(confirm(msg)){
            $.ajax({
                type : 'post',
                url : $(obj).attr('data-url'),
                data : {act:'del',del_id:$(obj).attr('data-id')},
                dataType : 'json',
                success : function(data){
                    if(data.status==1){
                        if(data.info){
                            ndialog.show(data.info);
                        }else{
                            ndialog.show('删除成功');
                        }
                        if(succFunc) succFunc();
                    }else{
                        if(data.info){
                            ndialog.show(data.info);
                        }else{
                            ndialog.show('删除失败');
                        }
                        if(errFunc) errFunc();
                    }
                }
            })
        }
        return false;
    }
}


/**
 *  Ajax通用提交表单
 *  @var  form表单的id属性值  form_id
 *  @var  提交地址 subbmit_url
 */
function post_form(form_id, subbmit_url) {
    if (form_id == '') {
        alert('缺少必要参数');
        return false;
    }
    if (!subbmit_url) {
        //  默认取当前地址  加上ajax请求标示
        subbmit_url = location.href + '/is_ajax/1';
    }
    //  序列化表单值
    var data = $('#' + form_id).serialize();

    $.post(subbmit_url, data, function (result) {
        var obj = $.parseJSON(result);
        if (obj.status == 0) {
            //alert(obj.msg);
            return false;
        }
        if (obj.status == 1) {
            //alert(obj.msg);
            if (obj.data.return_url) {
                //  返回跳转链接
                location.href = obj.data.return_url;
            } else {
                //  刷新页面
                location.reload();
            }
            return;
        }
    })
}

var ntable = {
    changeVal: function (table,id_name,id_value,field,obj) {
        var src = "";
        if($(obj).attr('src').indexOf("cancel.png") > 0 )
        {
            src = '/Public/images/yes.png';
            var value = 1;

        }else{
            src = '/Public/images/cancel.png';
            var value = 0;
        }
        $.ajax({
            url:"/Ms/Common/changeTableVal?table="+table+"&id_name="+id_name+"&id_value="+id_value+"&field="+field+'&value='+value,
            success: function(data){
                ndialog.show('更新成功');
                $(obj).attr('src',src);
            }
        });
    },
    updateSort: function(table,id_name,id_value,field,obj)
    {
        var value = $(obj).val();
        $.ajax({
            url:"/Ms/Common/changeTableVal?table="+table+"&id_name="+id_name+"&id_value="+id_value+"&field="+field+'&value='+value,
            success: function(data){
                layer.msg('更新成功', {icon: 1});
            }
        });
    }
}

function ajax_get_more(formId,page,url){
    $.ajax({
        type : "POST",
        url:url+"?p="+page,
        data : $('#'+formId).serialize(),
        success: function(data){
            $("#ajax_return").html('');
            $("#ajax_return").append(data);
        }
    });
}